package com.zhang.consumer;

import com.rabbitmq.client.Channel;
import com.zhang.client.PaymentFeignClient;
import com.zhang.constant.MqConst;
import com.zhang.entity.OrderInfo;
import com.zhang.entity.PaymentInfo;
import com.zhang.enums.ProcessStatus;
import com.zhang.service.PaymentInfoService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

public class PaymentConsumer {
    @Autowired
    private PaymentInfoService paymentInfoService;
    //2.关闭支付表单
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.CLOSE_PAYMENT_QUEUE,durable = "false"),
            exchange = @Exchange(value = MqConst.CLOSE_PAYMENT_EXCHANGE,durable = "false"),
            key = {MqConst.CLOSE_PAYMENT_ROUTE_KEY}))
    public void updateOrderAfterPaySuccess(String outTradeNo, Channel channel, Message message) throws Exception{
        if (!StringUtils.isEmpty(outTradeNo)){
            PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo);
            paymentInfo.setPaymentStatus(ProcessStatus.CLOSED.name());
            paymentInfoService.updateById(paymentInfo);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
